package com.activiti.candidate;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import java.util.List;

/**
 * 组任务
 *
 * @author ldanniel
 */
public class CandidateTask {
    public static void main(String[] args) {
        CandidateTask candidateTask = new CandidateTask();
        candidateTask.taskHandover();
    }
    public void deployment(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                //禁用架构验证
                .disableSchemaValidation()
                .name("candidateProcess")
                .addClasspathResource("bpmn/evection-candidate.bpmn")
                .deploy();
        System.out.println("部署流程的id" + deploy.getId());
        //部署流程的名称为空，是因为压缩包中可能有多个流程图，所以不能统一命名
        System.out.println("部署流程的名称" + deploy.getName());
    }
    public void startProcess(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //启动流程
        runtimeService.startProcessInstanceByKey("candidateProcess");
    }

    /**
     * 查询组任务
     */
    public void queryGroupTaskList(){
        String deploymentKey = "candidateProcess";
        String candidateUser = "李经理";
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        //任务查询器
        TaskQuery taskQuery = taskService.createTaskQuery();
        taskQuery.processDefinitionKey(deploymentKey);
        //根据候选人查询任务
        taskQuery.taskCandidateUser(candidateUser);
        List<Task> taskList = taskQuery.list();
        for (Task task : taskList) {
            System.out.println("任务名称 == " + task.getName());
            System.out.println("任务id == " + task.getId());
        }
    }


    /**
     * 领（拾）取任务
     */
    public void acceptTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        String taskId = "32502";
        String userId = "李经理";
        //这里采用了建造者模式，可以参考一下
        TaskQuery taskQuery = taskService.createTaskQuery();
        taskQuery.taskId(taskId);
        taskQuery.taskCandidateUser(userId);
        Task task = taskQuery.singleResult();
        if(null != task){
            //领取任务
            taskService.claim(taskId,userId);
            System.out.println("taskId = 【" + taskId +"】的任务，已被【"+userId + "】领取");
        }
    }

    /**
     * 归还(释放)任务
     */
    public void returnTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        String taskId = "32502";
        String assignee = "李经理";
        //这里采用了建造者模式，可以参考一下
        TaskQuery taskQuery = taskService.createTaskQuery();
        taskQuery.taskId(taskId);
        taskQuery.taskAssignee(assignee);
        Task task = taskQuery.singleResult();
        if(null != task){
            //归还任务(就是将act_ru_task表中的assignee_这一列的设置为空即可)
            taskService.setAssignee(taskId,null);
            System.out.println("taskId = 【" + taskId +"】的任务，已被【"+assignee + "】归还");
        }
    }

    /**
     * 任务交接
     */
    public void taskHandover(){
        //拾取任务
        this.acceptTask();

        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        String taskId = "32502";
        String assignee = "李经理";
        String userId = "赵经理";
        //这里采用了建造者模式，可以参考一下
        TaskQuery taskQuery = taskService.createTaskQuery();
        taskQuery.taskId(taskId);
        taskQuery.taskAssignee(assignee);
        Task task = taskQuery.singleResult();
        if(null != task){
            //交接任务(就是将act_ru_task表中的assignee_这一列的设置为交接人，即可)
            taskService.setAssignee(taskId,userId);
            System.out.println("taskId = 【" + taskId +"】的任务，已被交接给【"+userId + "】，并处理该任务");
        }
    }










    /**
     * 第一个分支，小于等于3天
     */
    public void commitProcess(){
        //创建
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        //通过任务服务接口获取，参数：流程id,负责人
        Task task = taskService.createTaskQuery()
                .processInstanceId("30001")
                .taskAssignee("张三")
                .singleResult();
        System.out.println("流程id = " + task.getProcessInstanceId());
        System.out.println("任务id = " + task.getId());
        System.out.println("负责人 = " + task.getAssignee());
        System.out.println("任务名称 = " + task.getName());
        //根据任务id完成任务
        taskService.complete(task.getId());
        System.out.println("执行任务完成");
    }
}
